unit DataReportImpl1;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ActiveX, AxCtrls, LMPrint_TLB, StdVcl, StdCtrls, xmldom,
  Provider, Xmlxform, DB, DBClient, RM_Dataset, ExtCtrls, RM_Common,
  RM_Preview, RM_System, RM_Class,Registry,IniFiles, RM_e_Xls, RM_BarCode;

type
  TDataReport = class(TActiveForm, IDataReport)
    RMReport1: TRMReport;
    RMPreview1: TRMPreview;
    RMDBDataSet1: TRMDBDataSet;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    XMLTransformProvider1: TXMLTransformProvider;
    XMLTransformProvider2: TXMLTransformProvider;
    ClientDataSet2: TClientDataSet;
    RMDBDataSet2: TRMDBDataSet;
    DataSource2: TDataSource;
    XMLTransformProvider3: TXMLTransformProvider;
    ClientDataSet3: TClientDataSet;
    RMDBDataSet3: TRMDBDataSet;
    DataSource3: TDataSource;
    Panel1: TPanel;
    Label1: TLabel;
    rowCountCB: TComboBox;
    RMXLSExport1: TRMXLSExport;
    RMBarCodeObject1: TRMBarCodeObject;
    procedure ActiveFormCreate(Sender: TObject);
    procedure rowCountCBChange(Sender: TObject);
    procedure ActiveFormDestroy(Sender: TObject);
  private
    { Private declarations }
    appPath:String;
    FEvents: IDataReportEvents;
    procedure ActivateEvent(Sender: TObject);
    procedure ClickEvent(Sender: TObject);
    procedure CreateEvent(Sender: TObject);
    procedure DblClickEvent(Sender: TObject);
    procedure DeactivateEvent(Sender: TObject);
    procedure DestroyEvent(Sender: TObject);
    procedure KeyPressEvent(Sender: TObject; var Key: Char);
    procedure PaintEvent(Sender: TObject);
  protected
    { Protected declarations }
    procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
    procedure EventSinkChanged(const EventSink: IUnknown); override;
    function Get_Active: WordBool; safecall;
    function Get_AlignDisabled: WordBool; safecall;
    function Get_AutoScroll: WordBool; safecall;
    function Get_AutoSize: WordBool; safecall;
    function Get_AxBorderStyle: TxActiveFormBorderStyle; safecall;
    function Get_Caption: WideString; safecall;
    function Get_Color: OLE_COLOR; safecall;
    function Get_DoubleBuffered: WordBool; safecall;
    function Get_DropTarget: WordBool; safecall;
    function Get_Enabled: WordBool; safecall;
    function Get_Font: IFontDisp; safecall;
    function Get_HelpFile: WideString; safecall;
    function Get_KeyPreview: WordBool; safecall;
    function Get_PixelsPerInch: Integer; safecall;
    function Get_PrintScale: TxPrintScale; safecall;
    function Get_Scaled: WordBool; safecall;
    function Get_ScreenSnap: WordBool; safecall;
    function Get_SnapBuffer: Integer; safecall;
    function Get_Visible: WordBool; safecall;
    function Get_VisibleDockClientCount: Integer; safecall;
    procedure _Set_Font(var Value: IFontDisp); safecall;
    procedure Set_AutoScroll(Value: WordBool); safecall;
    procedure Set_AutoSize(Value: WordBool); safecall;
    procedure Set_AxBorderStyle(Value: TxActiveFormBorderStyle); safecall;
    procedure Set_Caption(const Value: WideString); safecall;
    procedure Set_Color(Value: OLE_COLOR); safecall;
    procedure Set_DoubleBuffered(Value: WordBool); safecall;
    procedure Set_DropTarget(Value: WordBool); safecall;
    procedure Set_Enabled(Value: WordBool); safecall;
    procedure Set_Font(const Value: IFontDisp); safecall;
    procedure Set_HelpFile(const Value: WideString); safecall;
    procedure Set_KeyPreview(Value: WordBool); safecall;
    procedure Set_PixelsPerInch(Value: Integer); safecall;
    procedure Set_PrintScale(Value: TxPrintScale); safecall;
    procedure Set_Scaled(Value: WordBool); safecall;
    procedure Set_ScreenSnap(Value: WordBool); safecall;
    procedure Set_SnapBuffer(Value: Integer); safecall;
    procedure Set_Visible(Value: WordBool); safecall;

    function Get_DataPack1: WideString; safecall;
    procedure Set_DataPack1(const Value: WideString); safecall;

    function Get_XtrFile1: WideString; safecall;
    procedure Set_XtrFile1(const Value: WideString); safecall;

    function Get_ReportFile: WideString; safecall;
    procedure Set_ReportFile(const Value: WideString); safecall;

    function Get_Show: WideString; safecall;
    procedure Set_Show(const Value: WideString); safecall;
    procedure Set_Print(const Value: WideString); safecall;
    function Get_Print: WideString; safecall;
    function Get_DataPack2: WideString; safecall;
    procedure Set_DataPack2(const Value: WideString); safecall;
    function Get_XtrFile2: WideString; safecall;
    procedure Set_XtrFile2(const Value: WideString); safecall;
    //function IDataReport.Get_DataPack2 = IDataReport_Get_DataPack2;
    //function IDataReport_Get_DataPack2: WideString; safecall;
    //procedure IDataReport.Set_DataPack2 = IDataReport_Set_DataPack2;
    //procedure IDataReport_Set_DataPack2(const Value: WideString); safecall;
  public
    FReportName:String;
    procedure GetConfig(filepath:string;reportName:String;RMReport1: TRMReport);
    procedure SetComponent(value:string);
    procedure PageSetupChanged(Sender: TObject);
    procedure SetConfig(reportName:String;RMReport1: TRMReport);
    procedure SetPageRowCount(reportName:String;rows:Integer);
    { Public declarations }
    procedure Initialize; override;
  end;

implementation

uses ComObj, ComServ, DataUnit;

{$R *.DFM}

{ TDataReport }

procedure TDataReport.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
begin
  { Define property pages here.  Property pages are defined by calling
    DefinePropertyPage with the class id of the page.  For example,
      DefinePropertyPage(Class_DataReportPage); }
end;

procedure TDataReport.EventSinkChanged(const EventSink: IUnknown);
begin
  FEvents := EventSink as IDataReportEvents;
  inherited EventSinkChanged(EventSink);
end;

procedure TDataReport.Initialize;
begin
  inherited Initialize;
  OnActivate := ActivateEvent;
  OnClick := ClickEvent;
  OnCreate := CreateEvent;
  OnDblClick := DblClickEvent;
  OnDeactivate := DeactivateEvent;
  OnDestroy := DestroyEvent;
  OnKeyPress := KeyPressEvent;
  OnPaint := PaintEvent;
end;

function TDataReport.Get_Active: WordBool;
begin
  Result := Active;
end;

function TDataReport.Get_AlignDisabled: WordBool;
begin
  Result := AlignDisabled;
end;

function TDataReport.Get_AutoScroll: WordBool;
begin
  Result := AutoScroll;
end;

function TDataReport.Get_AutoSize: WordBool;
begin
  Result := AutoSize;
end;

function TDataReport.Get_AxBorderStyle: TxActiveFormBorderStyle;
begin
  Result := Ord(AxBorderStyle);
end;

function TDataReport.Get_Caption: WideString;
begin
  Result := WideString(Caption);
end;

function TDataReport.Get_Color: OLE_COLOR;
begin
  Result := OLE_COLOR(Color);
end;

function TDataReport.Get_DoubleBuffered: WordBool;
begin
  Result := DoubleBuffered;
end;

function TDataReport.Get_DropTarget: WordBool;
begin
  Result := DropTarget;
end;

function TDataReport.Get_Enabled: WordBool;
begin
  Result := Enabled;
end;

function TDataReport.Get_Font: IFontDisp;
begin
  GetOleFont(Font, Result);
end;

function TDataReport.Get_HelpFile: WideString;
begin
  Result := WideString(HelpFile);
end;

function TDataReport.Get_KeyPreview: WordBool;
begin
  Result := KeyPreview;
end;

function TDataReport.Get_PixelsPerInch: Integer;
begin
  Result := PixelsPerInch;
end;

function TDataReport.Get_PrintScale: TxPrintScale;
begin
  Result := Ord(PrintScale);
end;

function TDataReport.Get_Scaled: WordBool;
begin
  Result := Scaled;
end;

function TDataReport.Get_ScreenSnap: WordBool;
begin
  Result := ScreenSnap;
end;

function TDataReport.Get_SnapBuffer: Integer;
begin
  Result := SnapBuffer;
end;

function TDataReport.Get_Visible: WordBool;
begin
  Result := Visible;
end;

function TDataReport.Get_VisibleDockClientCount: Integer;
begin
  Result := VisibleDockClientCount;
end;

procedure TDataReport._Set_Font(var Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure TDataReport.ActivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnActivate;
end;

procedure TDataReport.ClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnClick;
end;

procedure TDataReport.CreateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnCreate;
end;

procedure TDataReport.DblClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDblClick;
end;

procedure TDataReport.DeactivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDeactivate;
end;

procedure TDataReport.DestroyEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDestroy;
end;

procedure TDataReport.KeyPressEvent(Sender: TObject; var Key: Char);
var
  TempKey: Smallint;
begin
  TempKey := Smallint(Key);
  if FEvents <> nil then FEvents.OnKeyPress(TempKey);
  Key := Char(TempKey);
end;

procedure TDataReport.PaintEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnPaint;
end;

procedure TDataReport.Set_AutoScroll(Value: WordBool);
begin
  AutoScroll := Value;
end;

procedure TDataReport.Set_AutoSize(Value: WordBool);
begin
  AutoSize := Value;
end;

procedure TDataReport.Set_AxBorderStyle(Value: TxActiveFormBorderStyle);
begin
  AxBorderStyle := TActiveFormBorderStyle(Value);
end;

procedure TDataReport.Set_Caption(const Value: WideString);
begin
  Caption := TCaption(Value);
end;

procedure TDataReport.Set_Color(Value: OLE_COLOR);
begin
  Color := TColor(Value);
end;

procedure TDataReport.Set_DoubleBuffered(Value: WordBool);
begin
  DoubleBuffered := Value;
end;

procedure TDataReport.Set_DropTarget(Value: WordBool);
begin
  DropTarget := Value;
end;

procedure TDataReport.Set_Enabled(Value: WordBool);
begin
  Enabled := Value;
end;

procedure TDataReport.Set_Font(const Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure TDataReport.Set_HelpFile(const Value: WideString);
begin
  HelpFile := String(Value);
end;

procedure TDataReport.Set_KeyPreview(Value: WordBool);
begin
  KeyPreview := Value;
end;

procedure TDataReport.Set_PixelsPerInch(Value: Integer);
begin
  PixelsPerInch := Value;
end;

procedure TDataReport.Set_PrintScale(Value: TxPrintScale);
begin
  PrintScale := TPrintScale(Value);
end;

procedure TDataReport.Set_Scaled(Value: WordBool);
begin
  Scaled := Value;
end;

procedure TDataReport.Set_ScreenSnap(Value: WordBool);
begin
  ScreenSnap := Value;
end;

procedure TDataReport.Set_SnapBuffer(Value: Integer);
begin
  SnapBuffer := Value;
end;

procedure TDataReport.Set_Visible(Value: WordBool);
begin
  Visible := Value;
end;

function TDataReport.Get_DataPack1: WideString;
begin

end;

procedure TDataReport.Set_DataPack1(const Value: WideString);
var dataFile:string;
begin
  if (Trim(value)='') then Exit;
  dataFile := appPath+'\DataPack1.xml';
  Base64StrToFile(Value,dataFile);
  XMLTransformProvider1.XMLDataFile:= dataFile;
end;

function TDataReport.Get_XtrFile1: WideString;
begin

end;

procedure TDataReport.Set_XtrFile1(const Value: WideString);
var dataFile:string;
begin
  if (Trim(value)='') then Exit;
  dataFile := GetSysDir+'\DataPack1.xtr';
  Base64StrToFile(Value,dataFile);
  XMLTransformProvider1.TransformRead.TransformationFile:= dataFile;
end;

function TDataReport.Get_ReportFile: WideString;
begin

end;

procedure TDataReport.Set_ReportFile(const Value: WideString);
begin
  if (Trim(Value)='') then exit;
  RMReport1.LoadFromAnsiString(Value);
end;

function TDataReport.Get_Show: WideString;
begin

end;

function GetAppPath:String;
var ARegistry : TRegistry;
begin
  result:='';
  ARegistry:=TRegistry.Create;
  try
    ARegistry.RootKey := HKEY_LOCAL_MACHINE;
    if ARegistry.OpenKey('Software\\LMXC',false) then
    begin
      result:= ARegistry.ReadString('InstallPath');
    end;
  finally
    ARegistry.Free;
  end;
end;

procedure TDataReport.GetConfig(filepath:string;reportName:String;RMReport1: TRMReport);
var myinifile:Tinifile;
  value:string;
  configFileName:string;// = 'C:\work\tmp3\ReportConfig.ini';
  pageheight,pagewidth,pagesize,pagebin:integer;
  pageorientation:TRMPrinterOrientation;
begin
  configFileName:=filepath+'\ReportConfig.ini';
  myinifile:=Tinifile.Create(configFileName);
  try
    if (reportName='labelprint') or (reportName='xcxsd')  or (reportName='xcxsdi')  or (reportName='xcrkd') then
    begin
    value:=myinifile.Readstring(reportName,'pageRowCount','4');
    TRMBandmasterdata(RMReport1.FindObject('MasterData1')).LinesPerPage:=strtoint(value);
    
    value:=myinifile.Readstring(reportName,'mmMarginTop','150');
    TRMReportPage(RMReport1.Pages[0]).mmMarginTop:= strtoint(value)*100;

    value:=myinifile.Readstring(reportName,'mmMarginBottom','150');
    TRMReportPage(RMReport1.Pages[0]).mmMarginBottom:= strtoint(value)*100;

    value:=myinifile.Readstring(reportName,'mmMarginLeft','150');
    TRMReportPage(RMReport1.Pages[0]).mmMarginLeft:= strtoint(value)*100;

    value:=myinifile.Readstring(reportName,'mmMarginRight','150');
    TRMReportPage(RMReport1.Pages[0]).mmMarginRight:= strtoint(value)*100;

    value:=myinifile.Readstring(reportName,'mmPageWidth','2400');
    pagewidth:= strtoint(value);

    value:=myinifile.Readstring(reportName,'mmPageHeight','1150');
    pageheight:= strtoint(value);

    pagesize:=  TRMReportPage(RMReport1.Pages[0]).PageSize;
    pagebin:=   TRMReportPage(RMReport1.Pages[0]).PageBin;
    pageorientation :=  TRMReportPage(RMReport1.Pages[0]).PageOrientation;
    TRMReportPage(RMReport1.Pages[0]).ChangePaper(pagesize,pagewidth,pageheight,pagebin,pageorientation);
    end;
  finally
    myinifile.Free;
  end;
end;

procedure TDataReport.SetComponent(value:string);
begin
  if (value = 'xcxsd') or (value='xcxsdi') or (value = 'xcrkd') then
  begin
    rowCountCB.ItemIndex:= TRMBandmasterdata(RMReport1.FindObject('MasterData1')).LinesPerPage-1;
    rowCountCB.Visible := true;
  end
  else
  begin
    Panel1.Visible := false;
  end;

end;

procedure TDataReport.SetConfig(reportName:String;RMReport1: TRMReport);
var myinifile:Tinifile;
  configFileName:string;
begin
  configFileName:=appPath+'\ReportConfig.ini';
  myinifile:=Tinifile.Create(configFileName);
  try
    myinifile.writeinteger(reportName,'mmMarginTop',trunc(TRMReportPage(RMReport1.Pages[0]).mmMarginTop/100));
    myinifile.writeinteger(reportName,'mmMarginBottom',trunc(TRMReportPage(RMReport1.Pages[0]).mmMarginBottom/100));
    myinifile.writeinteger(reportName,'mmMarginLeft',trunc(TRMReportPage(RMReport1.Pages[0]).mmMarginLeft/100));
    myinifile.writeinteger(reportName,'mmMarginRight',trunc(TRMReportPage(RMReport1.Pages[0]).mmMarginRight/100));
    myinifile.writeinteger(reportName,'mmPageWidth',TRMReportPage(RMReport1.Pages[0]).PageWidth);
    myinifile.writeinteger(reportName,'mmPageHeight',TRMReportPage(RMReport1.Pages[0]).PageHeight);
  finally
    myinifile.Free;
  end;
end;

procedure TDataReport.SetPageRowCount(reportName:String;rows:Integer);
var myinifile:Tinifile;
  value:string;
  configFileName:string;
  pageheight,pagewidth,pagesize,pagebin:integer;
  pageorientation:TRMPrinterOrientation;
begin
  TRMBandmasterdata(RMReport1.FindObject('MasterData1')).LinesPerPage:= rows;
  configFileName:=appPath+'\ReportConfig.ini';
  myinifile:=Tinifile.Create(configFileName);
  try
    myinifile.writeinteger(reportName,'pageRowCount',rows);
  finally
    myinifile.Free;
  end;
end;

procedure TDataReport.PageSetupChanged(Sender: TObject);
begin
  SetConfig(FReportName,RMReport1);
  GetConfig(appPath,FReportName,RMReport1);
  RMReport1.ShowReport;
end;

procedure TDataReport.rowCountCBChange(Sender: TObject);
begin
  SetPageRowCount(FReportName,StrToInt(rowCountCB.Text));
  GetConfig(appPath,FReportName,RMReport1);
  RMReport1.ShowReport;
end;

procedure TDataReport.Set_Show(const Value: WideString);
begin
  if appPath <>'' then
  begin
    FReportName:= Value;
    if fileexists(appPath+'\'+Value+'1.xtr') then
       XMLTransformProvider1.TransformRead.TransformationFile:= appPath+'\'+Value+'1.xtr';
    if fileexists(appPath+'\'+Value+'2.xtr') then
      XMLTransformProvider2.TransformRead.TransformationFile:= appPath+'\'+Value+'2.xtr';
    RMReport1.LoadFromFile(appPath+'\'+Value+'.rmf');

    if (value='labelprint')  or (value = 'xcxsd') or (value = 'xcxsdi') or (value = 'xcrkd') then
    begin
      GetConfig(appPath,FReportName,RMReport1);
      SetComponent(Value);
      TRMVirtualPreview(RMPreview1).OnAfterPageSetup :=PageSetupChanged;
      rowCountCB.OnChange := rowCountCBChange;
      GetConfig(appPath,FReportName,RMReport1);
    end
    else Panel1.Visible :=false;

    if (value='labelprint')  then
    begin
      RMReport1.PreviewButtons := RMReport1.PreviewButtons - [pbPrint];
    end;

    RMReport1.ShowReport;
  end;
end;

procedure TDataReport.Set_Print(const Value: WideString);
begin
    if appPath <>'' then
  begin
    FReportName:= Value;
    if fileexists(appPath+'\'+Value+'1.xtr') then
       XMLTransformProvider1.TransformRead.TransformationFile:= appPath+'\'+Value+'1.xtr';
    if fileexists(appPath+'\'+Value+'2.xtr') then
      XMLTransformProvider2.TransformRead.TransformationFile:= appPath+'\'+Value+'2.xtr';
    RMReport1.LoadFromFile(appPath+'\'+Value+'.rmf');

    if (value='labelprint')  or (value = 'xcxsd') or (value = 'xcxsdi') or (value = 'xcrkd') then
    begin
      GetConfig(appPath,FReportName,RMReport1);
      SetComponent(Value);
      TRMVirtualPreview(RMPreview1).OnAfterPageSetup :=PageSetupChanged;
      rowCountCB.OnChange := rowCountCBChange;
      GetConfig(appPath,FReportName,RMReport1);
    end
    else Panel1.Visible :=false;
    RMReport1.PrintReport;
  end;
end;

procedure TDataReport.ActiveFormCreate(Sender: TObject);
begin
  appPath := GetAppPath;  
end;



function TDataReport.Get_Print: WideString;
begin

end;

function TDataReport.Get_DataPack2: WideString;
begin

end;

procedure TDataReport.Set_DataPack2(const Value: WideString);
var   dataFile :string;
begin
  if (Trim(value)='') then Exit;
  dataFile := appPath+'\DataPack2.xml';
  Base64StrToFile(Value,dataFile);
  XMLTransformProvider2.XMLDataFile:= dataFile;
end;

function TDataReport.Get_XtrFile2: WideString;
begin

end;

procedure TDataReport.Set_XtrFile2(const Value: WideString);
var   dataFile :string;
begin
    if (Trim(value)='') then Exit;
  dataFile := GetSysDir+'\DataPack2.xtr';
  Base64StrToFile(Value,dataFile);
  XMLTransformProvider2.TransformRead.TransformationFile:= dataFile;
end;
   {
function TDataReport.IDataReport_Get_DataPack2: WideString;
begin

end;

procedure TDataReport.IDataReport_Set_DataPack2(const Value: WideString);
begin

end;
    }
procedure TDataReport.ActiveFormDestroy(Sender: TObject);
begin
  //AxCtrls.ParkingWindow;
end;

initialization
  TActiveFormFactory.Create(
    ComServer,
    TActiveFormControl,
    TDataReport,
    Class_DataReport,
    1,
    '',
    OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
    tmApartment);
end.
